package com.pers.springcloud.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Date;

/**
 * Gateway全局过滤器
 *
 * @Author: 黄建
 * @Date: 2020/3/10 23:53
 */
@Slf4j
public class MyGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("*******com in MyGatewayFilter：   "+new Date());
        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
        //验证失败
        if (StringUtils.isBlank(uname)){
            log.info("********用户名为null，非法用户名，");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        //验证通过
        return chain.filter(exchange);
    }

    //设置filter的优先级
    @Override
    public int getOrder() {
        return 0;
    }
}
